package com.chentf.spring.annotation;

import com.chentf.spring.annotation.bean.Boss;
import com.chentf.spring.annotation.bean.Car;
import com.chentf.spring.annotation.bean.Color;
import com.chentf.spring.annotation.config.MainConfigOfAutowired;
import com.chentf.spring.annotation.dao.BookDao;
import com.chentf.spring.annotation.service.BookService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IOCTest_Autowired {

    @Test
    public void test01() {
        AnnotationConfigApplicationContext  applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAutowired.class);
//        BookService bean1 = applicationContext.getBean(BookService.class);
//        System.out.println(bean1);

        // 1.我们在BookService类中使用@Autowired注解注入的BookDao对象和直接从IOC容器中获取的BookDao对象是同一个对象.
//        BookDao bean2 = applicationContext.getBean(BookDao.class);
//        System.out.println(bean2);

        // 2.@Autowired注解默认是优先按照类型去容器中找对应的组件，找到就赋值；如果找到多个相同类型的组件，那么再将属性的名称作为组件的id，到IOC容器中进行查找

        // 这已然说明在Boss类中通过@Autowired注解获取到的Car对象和直接从IOC容器中获取到Car对象是同一个对象
        Boss boss = applicationContext.getBean(Boss.class);
        System.out.println(boss);
        Car car = applicationContext.getBean(Car.class);
        System.out.println(car);
        applicationContext.close();
    }

    @Test
    public void test02() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAutowired.class);
        Color color = applicationContext.getBean(Color.class);
        System.out.println(color);
        applicationContext.close();
    }
}
